package com.life.ai.server.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域配置类 CorsConfig
 *
 * @author kylewka
 * @since 2023/6/21
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*"); // 1允许任何方法（post、get等）
        config.addAllowedHeader("*"); // 2允许任何头
        // 不使用cookie
        config.addAllowedOrigin("*"); // 3允许任何域名使用

        // 使用cookie
        //config.addAllowedOriginPattern("*");
        //config.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

}
